<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户添加</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row>
        <el-col :span="14">
          <el-form ref="form" :model="form" label-width="80px" :rules="formRules">
            <el-form-item label="用户名" prop="username">
              <el-col :span="12">
                <el-input v-model="form.username" :disabled="false"></el-input>
              </el-col>
            </el-form-item>
            <el-form-item label="手机" prop="phone">
              <el-col :span="12">
                <el-input v-model="form.phone"></el-input>
              </el-col>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-col :span="12">
                <el-input v-model="form.email"></el-input>
              </el-col>
            </el-form-item>
            <el-form-item label="状态">
              <el-switch
                :active-value="1"
                :inactive-value="0"
                v-model="form.status"
              ></el-switch>
            </el-form-item>
            <el-form-item label="时间">
              <el-col :span="10">
                <el-date-picker
                  type="date"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="选择日期"
                  v-model="form.createTime"
                  style="width: 100%"
                ></el-date-picker>
              </el-col>
              <!-- <el-col class="line" :span="-1">-</el-col>
            <el-col :span="6">
            <el-time-picker placeholder="选择时间" v-model="form.date2" style="width: 100%;"></el-time-picker>
            </el-col> -->
            </el-form-item>
            <el-form-item label="拥有角色">
              <el-select placeholder="请选择">
                <el-option
                  disabled
                  :multiple="true"
                  collapse-tags
                  v-for="item in form.roles"
                  :key="item.id"
                  :label="item.roleDesc"
                >
                </el-option>

                <!-- :disabled="true" -->
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="addOrupdate()"
                >立即创建</el-button
              >
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        id: "",
        username: "",
        phone: "",
        email: "",
        status: 0,
        createTime: "",
        roles: [],
      },
      formRules:{
        username:[
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      //当路由路径存在id的传参时候，就调用方法查询teacher的信息回显
      //if(this.$route.params && this.$route.query.id ){
      if (this.$route.params && this.$route.params.id) {
        //const id = this.$route.query.id
        const id = this.$route.params.id;
        this.getUserInfo(id);
      } else {
        this.form = {};
      }
    },
    async getUserInfo(id) {
      const { data: result } = await this.$http.get(`/user/findUserById/${id}`);
      //console.log(result.data.user);
      this.form = result.data.user;
      //console.log(this.form.roles);
    },
    addOrupdate() {
      if (this.form.id) {
        this.updateUser();
      } else {
        this.addUser();
      }
    },
    async updateUser() {
      
    },
    async addUser() {
      const data = JSON.parse(JSON.stringify(this.form));
      const result = await this.$http.post(`/user/addUser`, this.form).then(response=>{
        console.log(response.data);
        if(response.status==200){
          this.$message({            
            type: 'success',
            message: '添加成功!'
          });
          this.$router.push({path: '/findAllUser'}); 
        }
      });
      console.log(result);
    },
  },
  watch: {
    //路由变化，方法就会执行
    $route(to, from) {
      this.init();
    },
  },
};
</script>

<style lang="less" scoped>
</style>